package com.ruoyi.task.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.constant.SysConstants;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.task.mapper.SysStepMapper;
import com.ruoyi.task.domain.SysStep;
import com.ruoyi.task.service.ISysStepService;

/**
 * 步骤信息Service业务层处理
 * 
 * @author YJF
 * @date 2022-11-29
 */
@Service
public class SysStepServiceImpl implements ISysStepService 
{
    @Autowired
    private SysStepMapper sysStepMapper;

    /**
     * 查询步骤信息
     * 
     * @param stepId 步骤信息主键
     * @return 步骤信息
     */
    @Override
    public SysStep selectSysStepByStepId(Long stepId)
    {
        SysStep  stepInfo= sysStepMapper.selectSysStepByStepId(stepId);
        List<Long> sameUuidGroup = Arrays.stream(stepInfo.getSameUuid().split(",")).map(s -> Long.parseLong(s.trim())).collect(Collectors.toList());
        List<Long> childUuidGroup = Arrays.stream(stepInfo.getChildUuid().split(",")).map(s -> Long.parseLong(s.trim())).collect(Collectors.toList());
        stepInfo.setSameUuidGroup(sameUuidGroup);
        stepInfo.setChildUuidGroup(childUuidGroup);
        return stepInfo;
    }

    /**
     * 查询步骤信息列表
     * 
     * @param sysStep 步骤信息
     * @return 步骤信息
     */
    @Override
    public List<SysStep> selectSysStepList(SysStep sysStep) {
        if (StringUtils.isNull(sysStep.getDelFlag())) {
            sysStep.setDelFlag(SysConstants.CONSTANT_STR0);
        }
        List<SysStep> stepList  = sysStepMapper.selectSysStepList(sysStep);
        if (stepList.size() == 0) {
            return stepList;
        }
        //步骤相同识别码
        List<String> sameUuids = stepList.stream().map(t->t.getSameUuid()).collect(Collectors.toList());
        //子步骤识别码
        List<String> childUuids = stepList.stream().map(t->t.getChildUuid()).collect(Collectors.toList());
        List<String> uuidAll = new ArrayList<>();
        uuidAll.addAll(sameUuids);
        uuidAll.addAll(childUuids);
        String uuidStr = StringUtils.join(uuidAll, ",");
        List<Long> uuidLong= Arrays.stream(uuidStr.split(",")).map(s->Long.parseLong(s.trim())).distinct().collect(Collectors.toList());
        //步骤识别码集合
        Long[] uuids = uuidLong.toArray(new Long[uuidLong.size()]);
        SysStep stepObj = new SysStep();
        stepObj.setUuids(uuids);
        //获取包含的步骤列表
        List<SysStep> stepResult = sysStepMapper.selectSysStepList(stepObj);

        //构建步骤名称
        for (SysStep step : stepList) {
            StringBuffer sameNameStr = new StringBuffer();
            StringBuffer childNameStr = new StringBuffer();
            String[] sameUuid = step.getSameUuid().split(",");
            String[] childUuid = step.getChildUuid().split(",");
            //相同步骤
            for (String same : sameUuid) {
                for (SysStep st : stepResult) {
                    if(st.getUuid().equals(Long.parseLong(same))){
                        sameNameStr.append(",");
                        sameNameStr.append(st.getStepName());
                    }
                }
            }
            if(StringUtils.isNotBlank(sameNameStr.toString())){
               step.setSameNameStr(sameNameStr.toString().substring(1));
            }

            //子步骤
            for (String child : childUuid) {
                for (SysStep st : stepResult) {
                    if(st.getUuid().equals(Long.parseLong(child))){
                        childNameStr.append(",");
                        childNameStr.append(st.getStepName());
                    }
                }
            }
            if(StringUtils.isNotBlank(childNameStr.toString())){
                step.setChildNameStr(childNameStr.toString().substring(1));
            }
        }
        return stepList;
    }

    /**
     * 新增步骤信息
     * 
     * @param sysStep 步骤信息
     * @return 结果
     */
    @Override
    public int insertSysStep(SysStep sysStep)
    {
        sysStep.setSameUuid(StringUtils.join(sysStep.getSameUuidGroup(),","));
        sysStep.setChildUuid(StringUtils.join(sysStep.getChildUuidGroup(),","));
        sysStep.setCreateTime(DateUtils.getNowDate());
        return sysStepMapper.insertSysStep(sysStep);
    }

    /**
     * 修改步骤信息
     * 
     * @param sysStep 步骤信息
     * @return 结果
     */
    @Override
    public int updateSysStep(SysStep sysStep)
    {
        sysStep.setSameUuid(StringUtils.join(sysStep.getSameUuidGroup(),","));
        sysStep.setChildUuid(StringUtils.join(sysStep.getChildUuidGroup(),","));
        sysStep.setUpdateTime(DateUtils.getNowDate());
        return sysStepMapper.updateSysStep(sysStep);
    }

    /**
     * 批量删除步骤信息
     * 
     * @param stepIds 需要删除的步骤信息主键
     * @return 结果
     */
    @Override
    public int deleteSysStepByStepIds(Long[] stepIds)
    {
        return sysStepMapper.deleteSysStepByStepIds(stepIds);
    }

    /**
     * 删除步骤信息信息
     * 
     * @param stepId 步骤信息主键
     * @return 结果
     */
    public int deleteSysStepByStepId(Long stepId)
    {
        return sysStepMapper.deleteSysStepByStepId(stepId);
    }

    /**
     * 根据故障ID更新步骤信息信息
     *
     * @param sysStep 步骤信息
     * @return 结果
     */
    @Override
    public int updateSysStepByStepIds(SysStep sysStep) {
        sysStep.setUpdateTime(DateUtils.getNowDate());
        return sysStepMapper.updateSysStepByStepIds(sysStep);
    }

}
